home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / scripts / mysql_install_db < prev    next >
Encoding:
Text File  |  2004-10-28  |  14.7 KB  |  426 lines

  1. #!/bin/sh
  2. # Copyright (C) 2002-2004 MySQL AB
  3. # For a more info consult the file COPYRIGHT distributed with this file.
  4.  
  5. # This scripts creates the privilege tables db, host, user, tables_priv,
  6. # columns_priv in the mysql database, as well as the func table.
  7. #
  8. # All unrecognized arguments to this script are passed to mysqld.
  9.  
  10. in_rpm=0
  11. windows=0
  12. defaults=""
  13. user=""
  14. case "$1" in
  15.     -IN-RPM)
  16.       in_rpm="1"; shift
  17.       ;;
  18. esac
  19. case "$1" in
  20.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  21.       defaults="$1"; shift
  22.       ;;
  23. esac
  24.  
  25. parse_arguments() {
  26.   # We only need to pass arguments through to the server if we don't
  27.   # handle them here.  So, we collect unrecognized options (passed on
  28.   # the command line) into the args variable.
  29.   pick_args=
  30.   if test "$1" = PICK-ARGS-FROM-ARGV
  31.   then
  32.     pick_args=1
  33.     shift
  34.   fi
  35.  
  36.   for arg do
  37.     case "$arg" in
  38.       --force) force=1 ;;
  39.       --basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  40.       --ldata=*|--datadir=*) ldata=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  41.       --user=*)
  42.         # Note that the user will be passed to mysqld so that it runs
  43.         # as 'user' (crucial e.g. if log-bin=/some_other_path/
  44.         # where a chown of datadir won't help)
  45.      user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  46.       --skip-name-resolve) ip_only=1 ;;
  47.       --verbose) verbose=1 ;;
  48.       --rpm) in_rpm=1 ;;
  49.       --windows) windows=1 ;;
  50.       *)
  51.         if test -n "$pick_args"
  52.         then
  53.           # This sed command makes sure that any special chars are quoted,
  54.           # so the arg gets passed exactly to the server.
  55.           args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  56.         fi
  57.         ;;
  58.     esac
  59.   done
  60. }
  61.  
  62. # Get first arguments from the my.cfg file, groups [mysqld] and
  63. # [mysql_install_db], and then merge with the command line arguments
  64. if test -x ./bin/my_print_defaults
  65. then
  66.   print_defaults="./bin/my_print_defaults"
  67. elif test -x ./extra/my_print_defaults
  68. then
  69.   print_defaults="./extra/my_print_defaults"
  70. elif test -x /usr/local/mysql/bin/my_print_defaults
  71. then
  72.   print_defaults="/usr/local/mysql/bin/my_print_defaults"
  73. elif test -x /usr/local/mysql/bin/mysql_print_defaults
  74. then
  75.   print_defaults="/usr/local/mysql/bin/mysql_print_defaults"
  76. else
  77.   print_defaults="my_print_defaults"
  78. fi
  79.  
  80. args=
  81. ldata=
  82. execdir=
  83. bindir=
  84. basedir=
  85. force=0
  86. parse_arguments `$print_defaults $defaults mysqld mysql_install_db`
  87. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  88.  
  89. test -z "$ldata" && ldata=/usr/local/mysql/var
  90. if test -z "$basedir"
  91. then
  92.   basedir=/usr/local/mysql
  93.   bindir=/usr/local/mysql/bin
  94.   execdir=/usr/local/mysql/libexec 
  95.   pkgdatadir=@pkgdatadir@
  96. else
  97.   bindir="$basedir/bin"
  98. if test -x "$basedir/libexec/mysqld"
  99. then
  100.   execdir="$basedir/libexec"
  101. elif test -x "/usr/local/mysql/libexec/mysqld"
  102. then
  103.   execdir="/usr/local/mysql/libexec"
  104. else
  105.   execdir="$basedir/bin"
  106. fi
  107. fi
  108.  
  109. mdata=$ldata/mysql
  110. mysqld=$execdir/mysqld
  111. mysqld_opt=""
  112. scriptdir=$bindir
  113.  
  114. if test "$windows" = 1
  115. then
  116.   mysqld="./sql/mysqld"
  117.   mysqld_opt="--language=./sql/share/english"
  118.   scriptdir="./scripts"
  119. fi
  120.  
  121. if test ! -x $mysqld
  122. then
  123.   if test "$in_rpm" = 1
  124.   then
  125.     echo "FATAL ERROR $mysqld not found!"
  126.     exit 1
  127.   else
  128.     echo "Didn't find $mysqld"
  129.     echo "You should do a 'make install' before executing this script"
  130.     exit 1
  131.   fi
  132. fi
  133.  
  134. # Try to determine the hostname
  135. hostname=`/bin/hostname`
  136.  
  137. # Check if hostname is valid
  138. if test "$windows" = 0 -a "$in_rpm" = 0 -a $force = 0
  139. then
  140.   resolved=`$bindir/resolveip $hostname 2>&1`
  141.   if [ $? -ne 0 ]
  142.   then
  143.     resolved=`$bindir/resolveip localhost 2>&1`
  144.     if [ $? -ne 0 ]
  145.     then
  146.       echo "Neither host '$hostname' nor 'localhost' could not be looked up with"
  147.       echo "$bindir/resolveip"
  148.       echo "Please configure the 'hostname' command to return a correct hostname."
  149.       echo "If you want to solve this at a later stage, restart this script with"
  150.       echo "the --force option"
  151.       exit 1
  152.     fi
  153.     echo "WARNING: The host '$hostname' could not be looked up with resolveip."
  154.     echo "This probably means that your libc libraries are not 100 % compatible"
  155.     echo "with this binary MySQL version. The MySQL daemon, mysqld, should work"
  156.     echo "normally with the exception that host name resolving will not work."
  157.     echo "This means that you should use IP addresses instead of hostnames"
  158.     echo "when specifying MySQL privileges !"
  159.   fi
  160. fi
  161.  
  162. if test "$ip_only" = "1"
  163. then
  164.   ip=`echo "$resolved" | awk '/ /{print $6}'`
  165.   hostname=$ip
  166. fi
  167.  
  168. # Create database directories mysql & test
  169.   if test ! -d $ldata; then mkdir $ldata; chmod 700 $ldata ; fi
  170.   if test ! -d $ldata/mysql; then mkdir $ldata/mysql;  chmod 700 $ldata/mysql ; fi
  171.   if test ! -d $ldata/test; then mkdir $ldata/test;  chmod 700 $ldata/test ; fi
  172.   if test -w / -a ! -z "$user"; then
  173.     chown $user $ldata $ldata/mysql $ldata/test;
  174.   fi
  175.  
  176. # Initialize variables
  177. c_d="" i_d=""
  178. c_h="" i_h=""
  179. c_u="" i_u=""
  180. c_f="" i_f=""
  181. c_t="" c_c=""
  182.  
  183. # Check for old tables
  184. if test ! -f $mdata/db.frm
  185. then
  186.   echo "Preparing db table"
  187.  
  188.   # mysqld --bootstrap wants one command/line
  189.   c_d="$c_d CREATE TABLE db ("
  190.   c_d="$c_d   Host char(60) binary DEFAULT '' NOT NULL,"
  191.   c_d="$c_d   Db char(64) binary DEFAULT '' NOT NULL,"
  192.   c_d="$c_d   User char(16) binary DEFAULT '' NOT NULL,"
  193.   c_d="$c_d   Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  194.   c_d="$c_d   Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  195.   c_d="$c_d   Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  196.   c_d="$c_d   Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  197.   c_d="$c_d   Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  198.   c_d="$c_d   Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  199.   c_d="$c_d   Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  200.   c_d="$c_d   References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  201.   c_d="$c_d   Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  202.   c_d="$c_d   Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  203.   c_d="$c_d   Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  204.   c_d="$c_d   Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  205.   c_d="$c_d PRIMARY KEY Host (Host,Db,User),"
  206.   c_d="$c_d KEY User (User)"
  207.   c_d="$c_d )"
  208.   c_d="$c_d comment='Database privileges';"
  209.   
  210.   i_d="INSERT INTO db VALUES ('%','test','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y');
  211.   INSERT INTO db VALUES ('%','test\_%','','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','Y');"
  212. fi
  213.  
  214. if test ! -f $mdata/host.frm
  215. then
  216.   echo "Preparing host table"
  217.  
  218.   c_h="$c_h CREATE TABLE host ("
  219.   c_h="$c_h  Host char(60) binary DEFAULT '' NOT NULL,"
  220.   c_h="$c_h  Db char(64) binary DEFAULT '' NOT NULL,"
  221.   c_h="$c_h  Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  222.   c_h="$c_h  Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  223.   c_h="$c_h  Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  224.   c_h="$c_h  Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  225.   c_h="$c_h  Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  226.   c_h="$c_h  Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  227.   c_h="$c_h  Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  228.   c_h="$c_h  References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  229.   c_h="$c_h  Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  230.   c_h="$c_h  Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  231.   c_h="$c_h  Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  232.   c_h="$c_h  Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  233.   c_h="$c_h  PRIMARY KEY Host (Host,Db)"
  234.   c_h="$c_h )"
  235.   c_h="$c_h comment='Host privileges;  Merged with database privileges';"
  236. fi
  237.  
  238. if test ! -f $mdata/user.frm
  239. then
  240.   echo "Preparing user table"
  241.  
  242.   c_u="$c_u CREATE TABLE user ("
  243.   c_u="$c_u   Host char(60) binary DEFAULT '' NOT NULL,"
  244.   c_u="$c_u   User char(16) binary DEFAULT '' NOT NULL,"
  245.   c_u="$c_u   Password char(16) binary DEFAULT '' NOT NULL,"
  246.   c_u="$c_u   Select_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  247.   c_u="$c_u   Insert_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  248.   c_u="$c_u   Update_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  249.   c_u="$c_u   Delete_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  250.   c_u="$c_u   Create_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  251.   c_u="$c_u   Drop_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  252.   c_u="$c_u   Reload_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  253.   c_u="$c_u   Shutdown_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  254.   c_u="$c_u   Process_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  255.   c_u="$c_u   File_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  256.   c_u="$c_u   Grant_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  257.   c_u="$c_u   References_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  258.   c_u="$c_u   Index_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  259.   c_u="$c_u   Alter_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  260.   c_u="$c_u   Show_db_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  261.   c_u="$c_u   Super_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  262.   c_u="$c_u   Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  263.   c_u="$c_u   Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  264.   c_u="$c_u   Execute_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  265.   c_u="$c_u   Repl_slave_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  266.   c_u="$c_u   Repl_client_priv enum('N','Y') DEFAULT 'N' NOT NULL,"
  267.   c_u="$c_u   ssl_type enum('','ANY','X509', 'SPECIFIED') DEFAULT '' NOT NULL,"
  268.   c_u="$c_u   ssl_cipher BLOB NOT NULL,"
  269.   c_u="$c_u   x509_issuer BLOB NOT NULL,"
  270.   c_u="$c_u   x509_subject BLOB NOT NULL,"
  271.   c_u="$c_u   max_questions int(11) unsigned DEFAULT 0  NOT NULL,"
  272.   c_u="$c_u   max_updates int(11) unsigned DEFAULT 0  NOT NULL,"
  273.   c_u="$c_u   max_connections int(11) unsigned DEFAULT 0  NOT NULL,"
  274.   c_u="$c_u   PRIMARY KEY Host (Host,User)"
  275.   c_u="$c_u )"
  276.   c_u="$c_u comment='Users and global privileges';"
  277.  
  278.   i_u="INSERT INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
  279.   INSERT INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
  280.   
  281.   REPLACE INTO user VALUES ('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
  282.   REPLACE INTO user VALUES ('$hostname','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0);
  283.   
  284.   INSERT INTO user (host,user) values ('localhost','');
  285.   INSERT INTO user (host,user) values ('$hostname','');"
  286. fi
  287.  
  288. if test ! -f $mdata/func.frm
  289. then
  290.   echo "Preparing func table"
  291.  
  292.   c_f="$c_f CREATE TABLE func ("
  293.   c_f="$c_f   name char(64) binary DEFAULT '' NOT NULL,"
  294.   c_f="$c_f   ret tinyint(1) DEFAULT '0' NOT NULL,"
  295.   c_f="$c_f   dl char(128) DEFAULT '' NOT NULL,"
  296.   c_f="$c_f   type enum ('function','aggregate') NOT NULL,"
  297.   c_f="$c_f   PRIMARY KEY (name)"
  298.   c_f="$c_f )"
  299.   c_f="$c_f   comment='User defined functions';"
  300. fi
  301.  
  302. if test ! -f $mdata/tables_priv.frm
  303. then
  304.   echo "Preparing tables_priv table"
  305.  
  306.   c_t="$c_t CREATE TABLE tables_priv ("
  307.   c_t="$c_t   Host char(60) binary DEFAULT '' NOT NULL,"
  308.   c_t="$c_t   Db char(64) binary DEFAULT '' NOT NULL,"
  309.   c_t="$c_t   User char(16) binary DEFAULT '' NOT NULL,"
  310.   c_t="$c_t   Table_name char(64) binary DEFAULT '' NOT NULL,"
  311.   c_t="$c_t   Grantor char(77) DEFAULT '' NOT NULL,"
  312.   c_t="$c_t   Timestamp timestamp(14),"
  313.   c_t="$c_t   Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL,"
  314.   c_t="$c_t   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
  315.   c_t="$c_t   PRIMARY KEY (Host,Db,User,Table_name),"
  316.   c_t="$c_t   KEY Grantor (Grantor)"
  317.   c_t="$c_t )"
  318.   c_t="$c_t   comment='Table privileges';"
  319. fi
  320.  
  321. if test ! -f $mdata/columns_priv.frm
  322. then
  323.   echo "Preparing columns_priv table"
  324.  
  325.   c_c="$c_c CREATE TABLE columns_priv ("
  326.   c_c="$c_c   Host char(60) binary DEFAULT '' NOT NULL,"
  327.   c_c="$c_c   Db char(64) binary DEFAULT '' NOT NULL,"
  328.   c_c="$c_c   User char(16) binary DEFAULT '' NOT NULL,"
  329.   c_c="$c_c   Table_name char(64) binary DEFAULT '' NOT NULL,"
  330.   c_c="$c_c   Column_name char(64) binary DEFAULT '' NOT NULL,"
  331.   c_c="$c_c   Timestamp timestamp(14),"
  332.   c_c="$c_c   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,"
  333.   c_c="$c_c   PRIMARY KEY (Host,Db,User,Table_name,Column_name)"
  334.   c_c="$c_c )"
  335.   c_c="$c_c   comment='Column privileges';"
  336. fi
  337.  
  338. if test -n "$user"; then
  339.   args="$args --user=$user"
  340. fi
  341.  
  342. echo "Installing all prepared tables"
  343. if eval "$mysqld $defaults $mysqld_opt --bootstrap --skip-grant-tables \
  344.          --basedir=$basedir --datadir=$ldata --skip-innodb --skip-bdb \
  345.          $args" << END_OF_DATA
  346. use mysql;
  347. $c_d
  348. $i_d
  349.  
  350. $c_h
  351. $i_h
  352.  
  353. $c_u
  354. $i_u
  355.  
  356. $c_f
  357. $i_f
  358.  
  359. $c_t
  360. $c_c
  361. END_OF_DATA
  362. then
  363.   echo ""
  364.   if test "$in_rpm" = "0"
  365.   then
  366.     echo "To start mysqld at boot time you have to copy support-files/mysql.server"
  367.     echo "to the right place for your system"
  368.     echo
  369.   fi
  370.   echo "PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !"
  371.   echo "To do so, start the server, then issue the following commands:"
  372.   echo "$bindir/mysqladmin -u root password 'new-password'"
  373.   echo "$bindir/mysqladmin -u root -h $hostname password 'new-password'"
  374.   echo "See the manual for more instructions."
  375.   #
  376.   # Print message about upgrading unless we have created a new db table.
  377.   if test -z "$c_d"
  378.   then
  379.     echo
  380.     echo "NOTE:  If you are upgrading from a MySQL <= 3.22.10 you should run"
  381.     echo "the $bindir/mysql_fix_privilege_tables. Otherwise you will not be"
  382.     echo "able to use the new GRANT command!"
  383.   fi
  384.   echo
  385.   if test "$in_rpm" = "0"
  386.   then
  387.     echo "You can start the MySQL daemon with:"
  388.     echo "cd /usr/local/mysql ; $bindir/mysqld_safe &"
  389.     echo
  390.     echo "You can test the MySQL daemon with the benchmarks in the 'sql-bench' directory:"
  391.     echo "cd sql-bench ; perl run-all-tests"
  392.     echo
  393.   fi
  394.   echo "Please report any problems with the /usr/local/mysql/bin/mysqlbug script!"
  395.   echo
  396.   echo "The latest information about MySQL is available on the web at"
  397.   echo "http://www.mysql.com"
  398.   echo "Support MySQL by buying support/licenses at https://order.mysql.com"
  399.   echo 
  400.   exit 0
  401. else
  402.   echo "Installation of grant tables failed!"
  403.   echo
  404.   echo "Examine the logs in $ldata for more information."
  405.   echo "You can also try to start the mysqld daemon with:"
  406.   echo "$execdir/mysqld --skip-grant &"
  407.   echo "You can use the command line tool"
  408.   echo "$bindir/mysql to connect to the mysql"
  409.   echo "database and look at the grant tables:"
  410.   echo
  411.   echo "shell> $bindir/mysql -u root mysql"
  412.   echo "mysql> show tables"
  413.   echo
  414.   echo "Try 'mysqld --help' if you have problems with paths. Using --log"
  415.   echo "gives you a log in $ldata that may be helpful."
  416.   echo
  417.   echo "The latest information about MySQL is available on the web at"
  418.   echo "http://www.mysql.com"
  419.   echo "Please consult the MySQL manual section: 'Problems running mysql_install_db',"
  420.   echo "and the manual section that describes problems on your OS."
  421.   echo "Another information source is the MySQL email archive."
  422.   echo "Please check all of the above before mailing us!"
  423.   echo "And if you do mail us, you MUST use the /usr/local/mysql/bin/mysqlbug script!"
  424.   exit 1
  425. fi
  426.